<?php
function InsertCustomTourMap()
{
    global $dbsql,$language;
    require_once "includes/php-captcha.inc.php";
    
    if (PhpCaptcha::Validate($_POST['SpamCode'])==false)
    {
        // AjaxTransfer('spam',getLang('Please enter the correct security code!'));
    }
  
	$ip              = $_SERVER['REMOTE_ADDR'];
	$posttime        = time();
	
	//Personal Details
	$cName 		    = gpc_getStringPost('cName');
    $cPhone 		    = gpc_getStringPost('cPhone');
    $Country 		    = gpc_getStringPost('cCountry');
    $cDatePicker 		= gpc_getStringPost('cDatePicker');
    $cNumber 		    = gpc_getStringPost('cNumber');
    $cComments 		    = gpc_getStringPost('cComments');
    $newsletter 		    = gpc_getStringPost('Newsletter')=='on'? true:false;
    $Email 		    = gpc_getStringPost('cEmail');
        
	$loc		    = gpc_getStringArray('loc');	
	$acc 			= gpc_getStringArray('acc'); 
	$act 			= gpc_getStringArray('act');
    
    $loc            = implode(', ',$loc);
    $acc            = implode(', ',$acc);
    $act            = implode(', ',$act);
    
    
    ImportClass('country');
    $CountryObj     = new Country();
    $Country        = $CountryObj->getNameByCode($Country);
    
    $cDatePickerT   = strtotime($cDatePicker);
    if($cDatePickerT>4444)
    {
        $cDatePicker= date('Y-m-d',$cDatePickerT);
    }
 	//insert to booking -------------------------------
	$sql_insert = "INSERT INTO `tblcustomtourmap` 
    (`cName`, `cEmail`, `cPhone`, `cCountry`, `dTravellingDate`, `iNumOfPeople`, `cComments`, `cLocations`, `cActivities`, `cAccommodation`, `iStatus`, `iPostDate`) 
    VALUES ('$cName', '$Email', '$cPhone', '$Country', '$cDatePicker', '$cNumber', '$cComments', '$loc', '$act', '$acc', 0, '$posttime')";
	
	$dbsql->query($sql_insert);	

	//--------------------send to email:------------------------
    
 	$message_mail='<table width="100%" style="border:1px solid #aaaaaa; >
				<tr bgcolor="#fafafa">
					<td width="20%" style="padding-left:10px;"><b>Name</b></td>
		            <td style="padding-left:10px;">'.$cName.'</td>
				</tr>
				<tr bgcolor="#fafafa">
					<td width="20%" style="padding-left:10px;"><b>Email</b></td>
		            <td style="padding-left:10px;">'.$Email.'</td>
				</tr>
				<tr bgcolor="#fafafa">
					<td width="20%" style="padding-left:10px;"><b>Phone</b></td>
		            <td style="padding-left:10px;">'.$cPhone.'</td>
				</tr>
				<tr bgcolor="#fafafa">
					<td width="20%" style="padding-left:10px;"><b>country</b></td>
		            <td style="padding-left:10px;">'.$Country.'</td>
				</tr>		
				<tr bgcolor="#fafafa">
					<td width="20%" style="padding-left:10px;"><b>Travelling Date</b></td>
		            <td style="padding-left:10px;">'.$cDatePicker.'</td>
				</tr>
				<tr bgcolor="#fafafa">
					<td width="20%" style="padding-left:10px;"><b>Num Of People</b></td>
		            <td style="padding-left:10px;">'.$cNumber.'</td>
				</tr>
				<tr bgcolor="#fafafa">
					<td width="20%" style="padding-left:10px;"><b>Comments</b></td>
					<td style="padding-left:10px;">'.$cComments.'</td>
				</tr>
				<tr bgcolor="#fafafa">
					<td width="20%" style="padding-left:10px;"><b>Locations</b></td>
					<td style="padding-left:10px;">'.$loc.'</td>
				</tr>
				<tr bgcolor="#fafafa">
					<td width="20%" style="padding-left:10px;"><b>Activities</b></td>
					<td style="padding-left:10px;">'.$act.'</td>
				</tr>									
				<tr bgcolor="#fafafa">
					<td width="20%" style="padding-left:10px;"><b>Accommodation</b></td>
					<td style="padding-left:10px;">'.$acc.'</td>
				</tr>			
			</table>';
   
	$message_mail .="<br><br>IP:".$ip;
	
    $emailconfig       = @getConfigEmail('Customtourmap');
    
    if($emailconfig==false)
    {
        @include('admin/source/emailtemp/functions.php');
        InsertConfigEmail('Customtourmap','Customtourmap','Customtourmap',$Email,$content);
        //AjaxTransfer('config','Email not config');
    }else
    {
        $subject                = $emailconfig->title;
        
     	//send to admin
     	$rs   = send_email($emailconfig->email,"",$subject,$message_mail);
     	if($rs==false)
        {
            //AjaxTransfer('ok','Send Email False please contact to admin');
        }
        
        //send to cus---------------------	  
        $emailconfig->content = str_ireplace('{NAME}',$cName,$emailconfig->content);  
    	$rs   = send_email($Email,"",$subject,$emailconfig->content);     	
    	if($rs==false)
        {
            AjaxTransfer('ok','Send Email False please contact to admin');
        }
    }
    
	
    
    
    
 	
    
	//save email newletter--------------------------------------------
	if($newsletter)
 	{
        @ImportClass('newsletter');
 		@regnewsletter($Email,$FullName);
 	}
    
    
    $datasuccess    = getConfig('CustomtourmapSuccess');
    if($datasuccess==false)
    {
        $datasuccess = '<hr>Dear '.$cName.',<br>
                    Thank you for contacting GRANDEUR Journeys !<br>
                    Your inquiry has been sent to one of our consultants who will contact you directly within one business day (or 24 hours). Please be advised that our consultants will contact you by email (if your inquiry is from oversea), by phone (if your inquiry is within country).<br> 
                    If you are not contacted within 24 hours, or your inquiry is urgent, please call our hotline + [84] 909 646 034 so that I can prioritize your inquiry.
                    Best regards,<br>
                    The team at GRANDEUR Journeys';
    }
    $datasuccess    = str_ireplace('{NAME}',$cName,$datasuccess);
    
	AjaxTransfer('ok',$datasuccess);
}

function listCustomField($catid = 0)
{
    global $dbsql,$language;
    
    $sql_select = "select * from tblcustomfield where iType = $catid order by iOrder";
    return $dbsql->load($sql_select);
}